<template>
    <div class="login-wrapper">
        <!-- 欢迎标题 -->
        <div class="header">
            <h2>登录</h2>
            <p class="secondary-text">请输入用户名和密码进行登录</p>
        </div>

        <!-- 登录表单 -->
        <el-form :model="form" :rules="rules" ref="formRef" class="form">
            <el-form-item prop="username">
                <el-input
                        v-model="form.username"
                        placeholder="用户名 / 邮箱"
                        :prefix-icon="User"
                        clearable
                />
            </el-form-item>

            <el-form-item prop="password">
                <el-input
                        v-model="form.password"
                        type="password"
                        placeholder="密码"
                        :prefix-icon="Lock"
                        show-password
                        clearable
                />
            </el-form-item>

            <div class="form-footer">
                <el-checkbox v-model="form.remember">记住我</el-checkbox>
                <el-link
                        @click="router.push('/reset')"
                        class="secondary-text clickable"
                >忘记密码?
                </el-link
                >
            </div>

            <el-button class="login-btn" type="success" @click="userLogin()" plain>
                立即登录
            </el-button>
        </el-form>

        <!-- 注册提示 -->
        <el-divider>
            <span class="secondary-text">没有账号?</span>
        </el-divider>
        <el-button class="register-btn" type="warning" plain @click="router.push('/register')">
            立即注册
        </el-button>
    </div>
</template>

<script setup>
import {reactive, ref} from "vue"
import {Lock, User} from "@element-plus/icons-vue"
import {login} from "@/net/index.js"
import router from "@/router/index.js"
import {ElMessage} from "element-plus"

const formRef = ref()

const form = reactive({
    username: "",
    password: "",
    remember: false,
})

const rules = {
    username: [{required: true, message: "请输入用户名"}],
    password: [{required: true, message: "请输入密码"}],
}

function userLogin() {
    formRef.value.validate((valid) => {
        if (valid) {
            login(form.username, form.password, form.remember, () =>
                router.push("/index")
            )
        } else {
            ElMessage.warning("请填写完整信息")
        }
    })
}
</script>

<style scoped>
.login-wrapper {
    max-width: 21.25rem;
    margin: 0 auto;
    padding: 1.25rem;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
    height: 100%;
}

.header {
    text-align: center;
    margin-bottom: 1.5rem;
}

.header h2 {
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

/* 统一次要文字颜色 */
.secondary-text {
    color: var(--el-text-color-secondary);
    font-size: 0.875rem;
    transition: color 0.3s;
}

/* 忘记密码 鼠标悬停变蓝 + 手型 */
.clickable:hover {
    color: var(--el-color-primary);
    cursor: pointer;
}

.form {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
}

/* 穿透修改 el-input 样式 */
::v-deep(.el-input__wrapper) {
    border-radius: 0.625rem;
    height: 2.625rem;
    font-size: 0.875rem;
}

.form-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.8125rem;
    margin-top: -0.5rem;
    margin-bottom: 1rem;
}

.login-btn,
.register-btn {
    width: 100%;
    height: 2.625rem;
    border-radius: 0.625rem;
    font-size: 0.9375rem;
    margin-top: 0.75rem;
}

@media screen and (max-width: 30rem) {
    .login-wrapper {
        padding: 1rem;
        max-width: 100%;
    }

    .header h2 {
        font-size: 1.25rem;
    }

    .header p {
        font-size: 0.8125rem;
    }

    .form-footer {
        flex-direction: row;
        justify-content: space-around;
        align-items: center;
    }

    .form-footer .clickable {
        margin-top: 0;
    }
}
</style>